package com.doradance.entity;

import android.app.Activity;
import android.graphics.Color;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.cncn.youmengsharelib.ShareListener;
import com.cncn.youmengsharelib.SharePlatform;
import com.cncn.youmengsharelib.ui.SharePlatformsFragment;
import com.doradance.R;
import com.doradance.account.utils.ShareUtil;
import com.doradance.app.home.CoursesDetailActivity;
import com.pachong.android.frameworkbase.utils.EasyToast;

import java.util.List;

/**
 * Created by Administrator on 2018/3/16.
 */

public class CoursesDetailAdapter extends BaseAdapter{
    private int mCurrentItem=0;
    private boolean isClick=false;
    List<CoursesIdEntity.ChaptersBean> chapters;
    private int vip_limit;
    private int user_limit;
    private int id;
    private int courseId;
    CoursesIdEntity comments;
    FragmentManager supportFragmentManager;
    Activity mActivity;
    public CoursesDetailAdapter(FragmentManager supportFragmentManager, Activity activity, List<CoursesIdEntity.ChaptersBean> chapters, CoursesIdEntity comments, int vip_limit, int user_limit,int courseId){
        this.chapters = chapters;
        mActivity=activity;
        this.vip_limit =vip_limit;
        this.user_limit =user_limit;
        this.comments =comments;
        this.courseId=courseId;
        this.supportFragmentManager =supportFragmentManager;
    }
    @Override
    public int getCount() {
        return chapters.size();
    }

    @Override
    public Object getItem(int i) {
        return chapters.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (view==null){
            view= LayoutInflater.from(mActivity).inflate(R.layout.add_course_item,null);
        }
       TextView tvCourseTitle = (TextView) view.findViewById(R.id.tv_course_title);
       TextView tvShareFree = (TextView) view.findViewById(R.id.tv_share_free);
       TextView tvDetailProgress = (TextView) view.findViewById(R.id.tv_detail_progress);
        tvCourseTitle.setText(chapters.get(i).getName());
        if (chapters.get(i).isShare_free() && vip_limit - user_limit > 0 ) {
                tvShareFree.setVisibility(View.VISIBLE);
            }
            if(chapters.get(i).getProgress()!=0){
                tvDetailProgress.setVisibility(View.VISIBLE);
                tvDetailProgress.setText(chapters.get(i).getProgress_text());
            }
        if (mCurrentItem==i&&isClick){
            tvCourseTitle.setTextColor(Color.parseColor("#FF85A2"));
        }else{
            tvCourseTitle.setTextColor(Color.parseColor("#7f7f7f"));
        }
        if(chapters.get(i).isAuthorize()==true){
            tvShareFree.setVisibility(View.GONE);
        }else{
            tvShareFree.setVisibility(View.VISIBLE);
        }

        tvShareFree.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    id = chapters.get(i).getId();
                    ShareUtil.setShareChapterId(id);
                    ShareUtil.setShareCourseId(courseId);
                    Log.e("asasasasas",id+"df"+courseId);
//                    shareCharpte(comments.getName(),comments.getSummary(),comments.getCover(),"http://h5.doradance.cn/#/courseDetail?id=" +courseId);
                    mOnShareFreeClickListener.onClick(comments.getName(),comments.getSummary(),comments.getCover(),"http://h5.doradance.cn/#/courseDetail?id=" +courseId);
                }
            });
        return view;
    }

    public void setCurrentItem(int currentItem){
        this.mCurrentItem=currentItem;
    }
    public void setClick(boolean click){
        this.isClick=click;
    }

    private OnShareFreeClickListener mOnShareFreeClickListener;

    public interface OnShareFreeClickListener {
        void onClick(String title, String content, String image, String url);
    }
    public void setOnShareFreeClickListener(OnShareFreeClickListener onShareFreeClickListener){
        mOnShareFreeClickListener=onShareFreeClickListener;
    }


}
